package com.example.ktpspringboot.service.impl;

import com.example.ktpspringboot.entity.Student;
import com.example.ktpspringboot.entity.Teacher;
import com.example.ktpspringboot.mapper.StudentMapper;
import com.example.ktpspringboot.mapper.TeacherMapper;
import com.example.ktpspringboot.service.RegisterService;
import com.example.ktpspringboot.utils.SnowflakeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class RegisterServiceImpl implements RegisterService {

    private static final int ACCOUNT_LENGTH = 13;


    String judgePhone = "^\\d{11}$";
    String judgeEmail = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$";

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private SnowflakeUtil snowflakeUtil;

    @Override
    public String generateAccount() {
        String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("MMddHHmmss"));
        StringBuilder sb = new StringBuilder();
        Random random = new Random();

        sb.append("ktp").append(timestamp);

        while (sb.length() < ACCOUNT_LENGTH) {
            int digit = random.nextInt(10);
            sb.append(digit);
        }
        return sb.toString();
    }

    @Override
    public boolean dataVerification(String arg, String regex) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(arg);
        boolean matchesId = matcher.matches();
        if (arg.equals("")) {
            return false;
        }
        return matchesId;
    }

    @Override
    public String judgeTeaMethod(Teacher teacher) {
        if (dataVerification(teacher.getPhone(), judgePhone)) {
            Student student = studentMapper.queryByPhone(teacher.getPhone());
            if (student != null) {
                return "The user has been registered";
            } else {
                teacher.setUuid(String.valueOf(snowflakeUtil.generateId()));
                teacherMapper.insertTeacherByPhone(teacher);
                return "User registration succeeded";
            }
        }
        if (dataVerification(teacher.getPhone(), judgeEmail)) {
            Student student = studentMapper.queryByPhone(teacher.getPhone());
            if (student != null) {
                return "The user has been registered";
            } else {
                teacher.setEmail(teacher.getPhone());
                teacher.setUuid(String.valueOf(snowflakeUtil.generateId()));
                teacherMapper.insertTeacherByEmail(teacher);
                return "User registration succeeded";
            }
        }
        return "Phone is error";
    }

    @Override
    public String judgeStuMethod(Student student) {
        if (dataVerification(student.getPhone(), judgePhone)) {
            Student stu = studentMapper.queryByPhone(student.getPhone());
            if (stu != null) {
                return "The user has been registered";
            } else {
                student.setUuid(String.valueOf(snowflakeUtil.generateId()));
                studentMapper.insertStudentByPhone(student);
                return "User registration succeeded";
            }
        }
        if (dataVerification(student.getPhone(), judgeEmail)) {
            Student stu = studentMapper.queryByEmail(student.getPhone());
            if (stu != null) {
                return "The user has been registered";
            } else {
                student.setEmail(student.getPhone());
                student.setUuid(String.valueOf(snowflakeUtil.generateId()));
                studentMapper.insertStudentByEmail(student);
                return "User registration succeeded";
            }
        }
        return "Phone is error";
    }

}
